package com.example.yuejianapp14.Activity.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejianapp14.Activity.Adapter.TieziInfoAdapter;
import com.example.yuejianapp14.Activity.JavaBean.Comment;
import com.example.yuejianapp14.Activity.JavaBean.Success;
import com.example.yuejianapp14.Activity.JavaBean.Tiezi;
import com.example.yuejianapp14.Activity.JavaBean.User;
import com.example.yuejianapp14.Activity.Listener.ClubNoListListener;
import com.example.yuejianapp14.Activity.Listener.CommentListener;
import com.example.yuejianapp14.Activity.Listener.ListListener;
import com.example.yuejianapp14.Activity.Model.TieziModel;
import com.example.yuejianapp14.Activity.Model.UserModel;
import com.example.yuejianapp14.R;
import com.squareup.picasso.Picasso;

import java.util.List;

public class TieziInfoActivity extends AppCompatActivity {
    private ImageView back;
    private Button comment_fabu;
    private ImageView tiezi_pic,tiezi_user_pic;
    private TextView tiezi_content,tiezi_time,uname,add_tiezi_content;
    private Context mContext;
    private RecyclerView recyclerView;
    private EditText ac3;
    private SharedPreferences sp;
    String content;
//    String time;
    String a;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tiezi);
        initViews();
        getData();

        Intent intent = getIntent();
        a= intent.getStringExtra("tid");
//        content = ac3.getText().toString();
        Picasso.with(mContext).load("http://10.0.2.2/YueJian/Public/photo/tieziphoto/" + getIntent().getStringExtra("t_img")).into(tiezi_pic);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        comment_fabu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TieziModel tieziModel = new TieziModel();
                tieziModel.AddComment(sp.getString("uid",""),a,ac3.getText().toString(),AddCommentListener);
            }
        });
    }


    public void initViews() {
        back = (ImageView) findViewById(R.id.back);
        tiezi_pic = (ImageView) findViewById(R.id.tiezi_pic);
        tiezi_content = (TextView) findViewById(R.id.tiezi_content);
        tiezi_time = (TextView) findViewById(R.id.tiezi_time);
        uname = (TextView) findViewById(R.id.tiezi_user_name);
        tiezi_user_pic =(ImageView) findViewById(R.id.tiezi_user_pic);
        recyclerView = (RecyclerView)findViewById(R.id.rv_comment);
        recyclerView.setLayoutManager(new LinearLayoutManager((mContext)));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        ac3 = (EditText)findViewById(R.id.addcomment_content);
        comment_fabu = (Button)findViewById(R.id.comment_fabu);
        sp=getSharedPreferences("uid",MODE_PRIVATE);
    }

    ClubNoListListener<Tiezi> TieziInfoListener = new ClubNoListListener<Tiezi>() {
        @Override
        public void onResponse(Tiezi TieziInfo) {
            tiezi_content.setText(TieziInfo.getTcontent());
            tiezi_time.setText(TieziInfo.getTtime());
        }
        @Override
        public void onFail(String msg) {
        }
    };
    ClubNoListListener<User> UserInfoListener=new ClubNoListListener<User>() {
        public void onResponse(User userInfo) {
            uname.setText(userInfo.getUname());
            Picasso.with(mContext).load("http://10.0.2.2/YueJian/Public/photo/User/"+userInfo.getUpicture()).into(tiezi_user_pic);
        }
        @Override
        public void onFail(String msg) {
        }
    };

    CommentListener CommentListListener = new CommentListener() {
        @Override
        public void onResponse(List<Comment> beanList) {

            recyclerView.setAdapter(new TieziInfoAdapter(TieziInfoActivity.this,beanList));
        }

        @Override
        public void onFail(String msg) {

        }
    };
ListListener<Success> AddCommentListener=new ListListener<Success>() {
    @Override
    public void onResponse(Success a) {
        if(a.getSuccess().equals("1")){
        Toast.makeText(TieziInfoActivity.this, "发布成功",Toast.LENGTH_SHORT).show();
         Intent intent = getIntent();
         TieziModel tieziModel = new TieziModel();
         tieziModel.getComment(intent.getStringExtra("tid"), CommentListListener);
            ac3.setText("");
        }
        else {
            Toast.makeText(TieziInfoActivity.this, "发布失败",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(TieziInfoActivity.this, "fail",Toast.LENGTH_SHORT).show();
    }
};
    private void getData() {
        Intent intent = getIntent();
        TieziModel tieziModel = new TieziModel();
        UserModel userModel=new UserModel();
        tieziModel.TieziInfo(intent.getStringExtra("tid"), TieziInfoListener);
        tieziModel.getComment(intent.getStringExtra("tid"), CommentListListener);
        userModel.findUserinfo(intent.getStringExtra("uid"),UserInfoListener);
    }
}
